import React, { Component } from 'react';

import PageNav from '../../components/PageNav';

import { withRouter,NavLink,Switch,Redirect,Route} from 'react-router-dom';
import {observer,inject} from 'mobx-react';
// 导入需要的模块
@withRouter
@inject('obs','newobs')
@observer
class View extends Component {
    constructor(props){
        super(props)
        this.state = {
            dataList:[],
            pageConfig: {
                totalPage: 0, //总页码
                currentPage:sessionStorage.getItem('currentPage')||1
            },
            loading:true,
            pageNavCfg:{
                page_number: "1", 
                page_size: "10"
            }
        }
        this.getCurrentPage = this.getCurrentPage.bind(this);
    }

    newobs = this.props.newobs;

    getCurrentPage(currentPage){
        const cfg = this.state.pageNavCfg;
        cfg.page_number = currentPage.toString();
        const { getApi } = this.newobs;
        getApi('/home/mediareports',cfg,{}).then((res)=>{
            if(res.data.code==200){
                let {data,total} = res.data;
                this.setState({
                    loading:false,
                    dataList:data,
                    pageConfig: {
                        totalPage: Math.ceil(total/cfg.page_size), //总页码
                        currentPage:currentPage
                    }
                })
            }
        })
    }
    _currencyList(item){
        const list = item.map((val)=>(
            <li key={val.id} >
                <a href={val.jump_url}>{val.main_title}</a>
                <span>{val.source}</span>
                <span>{val.sourceDate}</span>
            </li>
        ))
        return list;
    }
    componentDidMount(){
        this.getCurrentPage(this.state.pageConfig.currentPage)
    }
    loading(){
        if(!this.state.loading){
            return this.state.dataList.length===0 ? <div className="no_result">小主~,此处空的可怜~</div> : <ul>{this._currencyList(this.state.dataList)}</ul>
        }else {
            return <div className="loading_box"><div className="loading"></div></div>
        }
    }
    render(){
        const pageCfg = {
            pageConfig:this.state.pageConfig,
            getCurrentPage:this.getCurrentPage
        }
        return(
            <React.Fragment>
                {this.loading()}
                {
                    this.state.dataList.length!==0?<PageNav {...pageCfg}/>:''
                }
            </React.Fragment>
        )
    }
}
export default View;
